﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using SharpMap.Layers;
using SharpMap.Forms;
using SharpMap.Data.Providers;
using System.IO;

namespace SharpMapApp
{
    public partial class LayerForm : DockContent
    {
        private readonly Dictionary<string, ILayerFactory> _layerFactoryCatalog =
            new Dictionary<string, ILayerFactory>();

        private MapImage m_mapImage;

        public LayerForm(MapImage mapImage)
        {
            InitializeComponent();
            RegisterLayerFactories();

            InitLayerView();

            m_mapImage = mapImage;
        }
        /// <summary>
        /// 注册图层工厂
        /// </summary>
        private void RegisterLayerFactories()
        {
            _layerFactoryCatalog[".shp"] = new ShapeFileLayerFactory();
        }
        /// <summary>
        /// 初始化图层树形结构
        /// </summary>
        private void InitLayerView()
        {
            layerView.BeginUpdate();
            layerView.CheckBoxes = true;
            layerView.Nodes.Clear();
            layerView.Nodes.Add("图层");
            layerView.EndUpdate();
            layerView.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.layerView_AfterCheck);
        }
        /// <summary>
        /// 添加图层
        /// </summary>
        /// <param name="layer"></param>
        public void AddLayer(ILayer layer)
        {
            // 排除重复载入图层
            if (m_mapImage.Map.GetLayerByName(layer.LayerName) == null)
            {
                m_mapImage.Map.Layers.Add(layer);
                m_mapImage.Refresh();
            }
        }
        /// <summary>
        /// 添加图层
        /// </summary>
        /// <param name="strLayerName"></param>
        public void AddLayer(string fileName)
        {
            string extension = Path.GetExtension(fileName);
            ILayerFactory layerFactory = null;
            if (!_layerFactoryCatalog.TryGetValue(extension.ToLower(), out layerFactory))
                return;

            string layerName = Path.GetFileNameWithoutExtension(fileName);
            ILayer layer = layerFactory.Create(layerName, fileName);
            
            AddLayer(layer);

            layerView.BeginUpdate();
            TreeNode rootNode = layerView.Nodes[0];
            TreeNode newNode = rootNode.Nodes.Add(layerName);
            newNode.Checked = true;
            rootNode.ExpandAll();
            layerView.EndUpdate();
        }
        /// <summary>
        /// 添加图层组
        /// </summary>
        /// <param name="fileNames"></param>
        public void AddLayer(string[] fileNames)
        {
            foreach (string fileName in fileNames)
            {
                AddLayer(fileName);
            }
        }
        /// <summary>
        /// 图层树形控件，选择/不选择图层将自动载入/卸载图层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void layerView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if ("图层目录".Equals(e.Node.Text))
            {
                foreach (TreeNode node in e.Node.Nodes)
                {
                    if (e.Node.Checked == node.Checked)
                        continue;
                    else
                        node.Checked = e.Node.Checked;
                }
            }
            else
            {
                if (e.Node.Checked)
                {
                    ILayer layer = new VectorLayer(e.Node.Text, 
                        new ShapeFile("../GeoData/World/" + e.Node.Text + ".shp", true));
                    AddLayer(layer);
                }
                else
                {
                    ILayer iLayer = m_mapImage.Map.GetLayerByName(e.Node.Text);
                    if (iLayer!=null)
                        m_mapImage.Map.Layers.Remove(iLayer);
                    m_mapImage.Refresh();
                }
            }
        }
    }
}
